package com.sxdx.service.impl;

import com.sxdx.common.Result;
import com.sxdx.entity.Book;
import com.sxdx.entity.LendList;
import com.sxdx.entity.dto.LendListDto;
import com.sxdx.mapper.BookMapper;
import com.sxdx.mapper.LendListMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxdx.service.ILendListService;
import com.sxdx.utils.UserHolder;
import com.sxdx.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 靳杰
 * @since 2023-12-22
 */
@Service
public class LendListServiceImpl extends ServiceImpl<LendListMapper, LendList> implements ILendListService {
    @Autowired
    private LendListMapper lendListMapper;
    @Autowired
    private BookMapper bookMapper;


    @Override
    public Result getLendList(LendListDto lendListDto) {
        String id= UserHolder.getUser().getUserId();
        LendListDto ld=new LendListDto();
        ld.setUserId(id);
        ld=lendListMapper.getRoleIdByUserId(ld);
//        roleId:0是管理，1是普通用户
        if (Integer.parseInt(ld.getRoleId())==1)
            return Result.success(lendListMapper.getLendList(ld));
        else
        {
            LendListDto newld=new LendListDto();
            return Result.success(lendListMapper.getLendList(newld));
        }
    }

    @Override
    public Result lendBook(LendList lendList) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");

        String id= UserHolder.getUser().getUserId();
        lendList.setLendDate(sdf.format(date));
        lendList.setUuid(UuidUtil.get32UUID());
        lendList.setReaderId(id);

        Book book =new Book();
        book.setUuid(lendList.getBookId());
        book = bookMapper.selectBookDetailById(book);

        int number = book.getNumber();
        int state = book.getState();
        if(number>1 && state == 0){
            number-=1;
            book.setNumber(number);
            book.setState(0); //0：在库
        }else if(number == 1 && state == 0){
            number-=1;
            book.setNumber(number);
            book.setState(1); //1：外借
        }else {
            return Result.error("库存不足，借阅失败");
        }

        if(lendListMapper.insertLendList(lendList) >0 && bookMapper.updateBookNumber(book)==1){
            return Result.success("借阅成功");
        }else {
            return Result.error("借阅失败");
        }
    }

    @Override
    public Result returnBook(LendList lendList) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        lendList.setBackDate(sdf.format(date));
        Book book =new Book();
        book.setUuid(lendList.getBookId());
        book = bookMapper.selectBookDetailById(book);
        int number = book.getNumber();
        number+=1;
        book.setNumber(number);
        int state = book.getState();
        if(state == 1){
            book.setState(0); //0：在库
        }
        if(lendListMapper.updateLendList(lendList) >0 && bookMapper.updateBookNumber(book)==1){
            return Result.success("还书成功");
        }else {
            return Result.error("还书失败");
        }
    }
}
